package utils.ioclasses;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class MultiPrintWriter extends PrintWriter {
    protected final List<PrintWriter> targetWriters = new ArrayList<>();
    public MultiPrintWriter(PrintWriter... writers) {
        super(new NullOutputStream()); // 使用一个空输出流，不实际写入任何内容
        Collections.addAll(targetWriters, writers);
    }

    @Override
    public void write(int c) {
        for (PrintWriter writer : targetWriters) {
            writer.write(c);
        }
    }

    @Override
    public void write(char[] buf, int off, int len) {
        for (PrintWriter writer : targetWriters) {
            writer.write(buf, off, len);
        }
    }
    @Override
    public void write(String s, int off, int len) {
        for (PrintWriter writer : targetWriters) {
            writer.write(s, off, len);
        }
    }

    @Override
    public void println() {
        for (PrintWriter writer : targetWriters) {
            writer.println();
        }
    }

    @Override
    public PrintWriter format(String format, Object ... args) {
        for (PrintWriter writer : targetWriters) {
            writer.format(format, args);
        }
        return this;
    }

    @Override
    public PrintWriter format(Locale l, String format, Object ... args) {
        for (PrintWriter writer : targetWriters) {
            writer.format(l, format, args);
        }
        return this;
    }

    @Override
    public void flush() {
        for (PrintWriter writer : targetWriters) {
            writer.flush();
        }
    }

    @Override
    public boolean checkError() {
        boolean ret = false;
        for (PrintWriter writer : targetWriters) {
            ret = writer.checkError() || ret;
        }
        return ret;
    }

    @Override
    public void close() {
        super.close();
    }

}
